#set -x  # 打开输出
emulate bash  # 模拟bash执行脚本
INPUT="$1"
INFO_RAW_LINE_TEMP=$'%{\C-[[01;31m%}$(emulate_zsh)%{\C-[[01;36m%}%n%{\C-[[37m%}@%{\C-[[32m%}%m%{\C-[[37m%}:%{\C-[[1m\C-[[01;34m%}%~%{\C-[[00m%}$(git_prompt_info) %(?,,C:%{\C-[[31m%}%?%{\C-[[00m%})'

OLD_IFS="$IFS"
IFS=$'\n'
if [ "${INPUT:0:1}" = $'\n' ]; then
  HEAD_EMPTY=1
  arr=('' $INPUT)  # 不能用引号，正是要利用IFS进行分割进组
else
  arr=($INPUT)
fi
IFS="$OLD_IFS"

LINE_NUMBER="${#arr[@]}"

for line_index in $(seq 1 "$LINE_NUMBER")
do
    RAW_LINE="${arr[$((line_index-1))]}"
    # 最后30个字符匹配INFO行，即 %(?,,C:%{^[[31m%}%?%{^[[00m%})
    if [ "${RAW_LINE:0-28}" = "${INFO_RAW_LINE_TEMP:0-28}" ]; then
        INFO_LINE_INDEX="$line_index"  # INFO行索引
        ORG_INFO_LINE="${arr[$((INFO_LINE_INDEX-1))]}"
        ENTRY_LINE_INDEX="$((INFO_LINE_INDEX+1))"
        ORG_ENTRY_LINE="${arr[$INFO_LINE_INDEX]}"
        break
    fi
done

if [ "$HEAD_EMPTY" = "1" ] && [ "$INFO_LINE_INDEX" = "2" ]; then
  PRE=''
else
  PRE="${arr[@]:0:$((INFO_LINE_INDEX-1))}"
  PRE=$(echo -n $PRE)" "
fi

if [ "$ENTRY_LINE_INDEX" = "$LINE_NUMBER" ]; then
  AFTER=''
else
  AFTER="${arr[@]:$ENTRY_LINE_INDEX}"
  AFTER=" "$(echo -n $AFTER)" "
fi

NEW_INFO_LINE="$PRE$ORG_INFO_LINE"
NEW_ENTRY_LINE=$(echo -n $ORG_ENTRY_LINE"$AFTER")

NEW_PROMPT="
$NEW_INFO_LINE
$NEW_ENTRY_LINE"

echo -n "$NEW_PROMPT"
